package com.geeka.d12.tcp_threadpool;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

public class TcpServerWithPool {
    // 创建线程池
    private static ExecutorService threadPool = new ThreadPoolExecutor(10, 30, 20, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());


    public static void main(String[] args) {
        int port = 9999;

        try (ServerSocket serverSocket = new ServerSocket(port);) {

            while (true) {
                // 阻塞等待client的连接
                Socket socket = serverSocket.accept();
                String cname = socket.getInetAddress() + " " + socket.getPort();
                System.out.println("有人连接上了 " + cname);
                System.out.println("server启动:" + port);
                // 利用线程池来配陪同者
                threadPool.execute(new SocketTask(socket));

            }

        } catch (IOException e) {
            System.out.println("发生异常" + e.getMessage());
            e.printStackTrace();
        }
    }
}
